<?php

namespace App\Http\Controllers;

use App\Models\AfterSales;
use App\Models\MakeMoney;
use App\Models\Order;
use App\Models\OrderGoods;
use App\Models\OrderInvoice;
use App\Service\WxPay;
use Illuminate\Http\Request;

class OrderController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     * 客户端订单列表
     */
    public function index(Request $request)
    {
        $res = $request->input();
        if (empty($res['user_id'])) {
            return $this->ResultJson(0, '用户ID不能为空!', []);
        }
        $result = (new Order())->getOrderList($res);
        if ($result) {
            return $this->ResultJson(1, '获取成功!', $result);
        }
        return [];
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     * 医护室订单列表
     */
    public function doctorOrderList(Request $request)
    {
        $res = $request->input();
        if (empty($res['work_id'])) {
            return $this->ResultJson(0, '工作室ID不能为空!', []);
        }
        $result = (new Order())->doctorOrderList($res);
        if ($result) {
            return $this->ResultJson(1, '获取成功!', $result);
        }
        return [];
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * 下单
     */
    public function create(Request $request)
    {
        $res = $request->input() ?: [];
        $param = $this->verify($res);
        if ($param === true) {
            $result = (new Order())->createOrder($res);
            if (is_array($result)) {
                return $this->ResultJson(1, '提交订单成功!', $result);
            }
            return $this->ResultJson(0, $result, []);
        }
        return $param;
    }

    /****
     * @param Request $request
     * 我的课程
     */
    public function getMyCourse(Request $request)
    {
        $user_id = $request->input('user_id');
        $result = (new Order())->getMyCourseList($user_id);
        return $this->ResultJson(1, '获取成功!', $result);
    }

    /****
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 数据验证
     */
    public function verify($res)
    {
        if (empty($res['order_type'])) {
            return $this->ResultJson(0, '订单类型不能为空!', []);
        }
        if ($res['order_type'] == 1) {  //课程订单
            if (empty($res['user_id'])) {
                return $this->ResultJson(0, '用户id不能为空!', []);
            }
            if (empty($res['goods_id'])) {
                return $this->ResultJson(0, '课程id不能为空!', []);
            }
            if (empty($res['order_price'])) {
                return $this->ResultJson(0, '订单价格不能为空!', []);
            }
            if (empty($res['pay_price'])) {
                return $this->ResultJson(0, '支付价格不能为空!', []);
            }
            if (empty($res['total_price'])) {
                return $this->ResultJson(0, '订单总价格不能为空!', []);
            }
            if (empty($res['work_id'])) {
                return $this->ResultJson(0, '工作室id不能为空!', []);
            }
        } elseif ($res['order_type'] == 2) {  //商品订单
            if (empty($res['user_id'])) {
                return $this->ResultJson(0, '用户id不能为空!', []);
            }
            if (empty($res['address_id'])) {
                return $this->ResultJson(0, '收货地址不能为空!', []);
            }
            if (empty($res['goods_id'])) {
                return $this->ResultJson(0, '商品id不能为空!', []);
            }
//            if (empty($res['goods_num'])) {
//                return $this->ResultJson(0, '商品数量不能为空!', []);
//            }
            if (empty($res['order_price'])) {
                return $this->ResultJson(0, '订单价格不能为空!', []);
            }
            if (empty($res['pay_price'])) {
                return $this->ResultJson(0, '支付价格不能为空!', []);
            }
            if (empty($res['total_price'])) {
                return $this->ResultJson(0, '订单总价格不能为空!', []);
            }
            if (empty($res['work_id'])) {
                return $this->ResultJson(0, '工作室id不能为空!', []);
            }
        } elseif ($res['order_type'] == 3 || $res['order_type'] == 4) { //图文预约和电话预约
            if (empty($res['user_id'])) {
                return $this->ResultJson(0, '用户id不能为空!', []);
            }
            if (empty($res['goods_id'])) {
                return $this->ResultJson(0, '商品id不能为空!', []);
            }
            if (empty($res['order_price'])) {
                return $this->ResultJson(0, '订单价格不能为空!', []);
            }
            if (empty($res['pay_price'])) {
                return $this->ResultJson(0, '支付价格不能为空!', []);
            }
            if (empty($res['total_price'])) {
                return $this->ResultJson(0, '订单总价格不能为空!', []);
            }
            if (empty($res['work_id'])) {
                return $this->ResultJson(0, '工作室id不能为空!', []);
            }
            if ($res['order_type'] == 4) {
                if (empty($res['phone'])) {
                    return $this->ResultJson(0, '电话不能为空!', []);
                }
                if (empty($res['rever_date'])) {
                    return $this->ResultJson(0, '预约日期不能为空!', []);
                }
                if (empty($res['rever_time'])) {
                    return $this->ResultJson(0, '预约时间不能为空!', []);
                }
            }
            if (empty($res['case_id'])) {
                return $this->ResultJson(0, '病例ID不能为空!', []);
            }
        }
        return true;
    }


    /**
     * 吹发货
     */
    public function urgeDelivery(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        $order = (new Order())::urgeDelivery($param['order_id']);
        if ($order == true) {
            return $this->ResultJson(1, '催发货成功!', []);
        }
        return $this->ResultJson(0, '催发货失败!', []);
    }











    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function applyInvoice(Request $request)
    {
        $param = $request->input();
        $data = $this->verifyInvoice($param);
        if ($data === true) {
            $result = (new OrderInvoice())->addInvoice($param);
            if ($result === true) {
                return $this->ResultJson(1, '申请成功!', []);
            }
            return $this->ResultJson(0, $result, []);
        }
        return $data;
    }

    public function verifyInvoice($res)
    {
        if (empty($res['order_id'])) {
            return $this->ResultJson(0, '订单ID不能为空!', []);
        }
//        if (empty($res['type'])) {
//            return $this->ResultJson(0, '发票类型不能为空!', []);
//        }
//        if (empty($res['content'])) {
//            return $this->ResultJson(0, '发票内容不能为空!', []);
//        }
        if (empty($res['head_type'])) {
            return $this->ResultJson(0, '抬头类型不能为空!', []);
        }
        if (empty($res['head_name'])) {
            return $this->ResultJson(0, '抬头名称不能为空!', []);
        }
        if ($res['head_type'] == 2) {
            if (empty($res['head_num'])) {
                return $this->ResultJson(0, '社会统一代码或者身份证号不能为空!', []);
            }
        }
        if (empty($res['name'])) {
            return $this->ResultJson(0, '名称不能为空!', []);
        }
        if (empty($res['phone'])) {
            return $this->ResultJson(0, '电话不能为空!', []);
        }
        if (empty($res['province_id'])) {
            return $this->ResultJson(0, '省Code不能为空!', []);
        }
        if (empty($res['city_id'])) {
            return $this->ResultJson(0, '市Code不能为空!', []);
        }
        if (empty($res['county_id'])) {
            return $this->ResultJson(0, '县Code不能为空!', []);
        }
        return true;
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Order $order
     * @return \Illuminate\Http\Response
     * 申请售后
     */
    public function addAfterSales(Request $request)
    {
        $param = $request->input();
        if(isset($param['id']) && !empty($param['id'])){
            $data = true;
        }else{
            $data = $this->verifyAfter($param);
        }
        if ($data === true) {
            $result = (new AfterSales())->addAfterSales($param);
            if ($result === true) {
                return $this->ResultJson(1, '申请成功!', []);
            }
            return $this->ResultJson(0, $result, []);
        }
        return $data;
    }

    /***
     * @param $res
     * @return bool|\Illuminate\Http\JsonResponse
     * 验证申请售后数据
     */
    public function verifyAfter($res)
    {
        if (empty($res['order_id'])) {
            return $this->ResultJson(0, '订单ID不能为空!', []);
        }
        if (empty($res['after_type'])) {
            return $this->ResultJson(0, '售后类型不能为空!', []);
        }
        if (empty($res['order_sn'])) {
            return $this->ResultJson(0, '订单号不能为空!', []);
        }
        if ($res['after_type'] == 1) {
            if (empty($res['goods_status'])) {
                return $this->ResultJson(0, '货物状态不能为空!', []);
            }
        } else {
//            if (empty($res['goods_num'])) {
//                return $this->ResultJson(0, '货物数量不能为空!', []);
//            }
//            if (empty($res['logis_company'])) {
//                return $this->ResultJson(0, '物流公司名称不能为空!', []);
//            }
//            if (empty($res['logis_code'])) {
//                return $this->ResultJson(0, '物流公司编码不能为空!', []);
//            }
//            if (empty($res['logis_num'])) {
//                return $this->ResultJson(0, '物流单号不能为空!', []);
//            }
            if (empty($res['address_id'])) {
                return $this->ResultJson(0, '寄件地址ID不能为空!', []);
            }
        }
        if (empty($res['back_reason'])) {
            return $this->ResultJson(0, '退货原因不能为空!', []);
        }
        if (empty($res['money'])) {
            return $this->ResultJson(0, '金额不能为空!', []);
        }
//        if (empty($res['images'])) {
//            return $this->ResultJson(0, '图片不能为空!', []);
//        }
        if (empty($res['describe'])) {
            return $this->ResultJson(0, '描述不能为空!', []);
        }
        return true;
    }


    public function addLogisNum(Request $request)
    {
        $res = $request->input();
        if (empty($res['after_id'])) {
            return $this->ResultJson(0, '售后ID不能为空!', []);
        }
        if (empty($res['logis_company'])) {
            return $this->ResultJson(0, '物流公司名称不能为空!', []);
        }
        if (empty($res['logis_num'])) {
            return $this->ResultJson(0, '物流单号不能为空!', []);
        }
        if (empty($res['logis_code'])) {
            return $this->ResultJson(0, '物流Code码不能为空!', []);
        }
        $result = (new AfterSales())->addLogisNum($res);
        if($result){
            return $this->ResultJson(1, '添加成功!', []);
        }
        return $this->ResultJson(0, '添加失败!', []);
        
    }



    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Order $order
     * @return \Illuminate\Http\Response
     */
    public function getOrderDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单id不能为空!", []);
        }
        $res = (new Order())::getOrderDetail($param['order_id']);
        return $this->ResultJson(1, "获取成功!", $res->toArray());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\Models\Order $order
     * @return \Illuminate\Http\Response
     * 确认收货
     */
    public function confirmReceipt(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单id不能为空!", []);
        }
        $res = (new Order())::confirmReceipt($param['order_id']);
        if ($res === true) {
            return $this->ResultJson(1, "确认收货成功!", []);
        }
        return $this->ResultJson(0, $res, []);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Order $order
     * @return \Illuminate\Http\Response
     * 支付
     * pay_type  1微信2支付宝3余额
     * types  1商品和课程2打赏3预约
     */
    public function doPay(Request $request)
    {
        $param = $request->input();
//        判断那种操作
        if (empty($param['pay_type'])) {
            return $this->ResultJson(0, "支付方式不能为空!", []);
        }
        if (empty($param['types'])) {
            return $this->ResultJson(0, "操作方式不能为空!", []);
        }
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        if (empty($param['user_id'])) {
            return $this->ResultJson(0, "用户ID不能为空!", []);
        }
        $order = (new Order())->where([['id','=',$param['order_id']]])->first();
        $order_goods = (new OrderGoods())->where([['order_id','=',$param['order_id']]])->first();
        (new Order())->where([['id','=',$param['order_id']]])->update(['pay_type'=>$param['pay_type']]);
        if ($param['types'] == 1 || $param['types'] == 2) {  //1商品和课程
            if ($param['pay_type'] == 1) {  //微信
                $data['sn'] = $order->sn;
                $data['des'] = '益鹿出院-商品订单支付';
                $data['price'] = $order->pay_price;
                $res = (new WxPayController())::setPay($data);
                if(is_array($res)){
                    return $this->ResultJson(1, "微信支付成功!", $res);
                }else{
                    return $this->ResultJson(0, "微信支付失败!", $res);
                }
            } elseif ($param['pay_type'] == 2) { //支付宝
                $datas['sn'] = $order->sn;
                $datas['pay_price'] = $order->pay_price;
                $datas['order_type'] = $order->order_type;
                $datas['subject'] = $order_goods->goods_name .'支付';
                $res = (new PaymentController())->doPay($datas);
                if($res){
                    return $this->ResultJson(1, "支付宝支付成功!", ['str'=>$res]);
                }
                return $this->ResultJson(0, "支付失败!", []);
            } elseif ($param['pay_type'] == 3) { //余额
                $makeMoney = new MakeMoney();
                $res = $makeMoney->doSurplusPay($param);
                if ($res === true) {
                    return $this->ResultJson(1, "余额支付成功!", []);
                }
                return $this->ResultJson(0, $res, []);
            }
        } elseif ($param['types'] == 3) { //3打赏
            if ($param['pay_type'] == 1) {  //微信
                $data['sn'] = $order->sn;
                $data['des'] = '益鹿出院-打赏订单支付';
                $data['price'] = $order->pay_price;
                $res = (new WxPayController())::setPay($data);
                if(is_array($res)){
                    return $this->ResultJson(1, "微信支付成功!", $res);
                }else{
                    return $this->ResultJson(0, "微信支付失败!", $res);
                }
            } elseif ($param['pay_type'] == 2) { //支付宝

            } elseif ($param['pay_type'] == 3) { //余额
                $makeMoney = new MakeMoney();
                $res = $makeMoney->doSurplusPay($param);
                if ($res === true) {
                    return $this->ResultJson(1, "余额支付成功!", []);
                }
                return $this->ResultJson(0, $res, []);
            }
        } elseif ($param['types'] == 4) { //4质询
            if ($param['pay_type'] == 1) {  //微信
                $data['sn'] = $order->sn;
                $data['des'] = '益鹿出院-咨询订单支付';
                $data['price'] = $order->pay_price;
                $res = (new WxPayController())::setPay($data);
                if(is_array($res)){
                    return $this->ResultJson(1, "微信支付成功!", $res);
                }else{
                    return $this->ResultJson(0, "微信支付失败!", $res);
                }
            } elseif ($param['pay_type'] == 2) { //支付宝
                $datas['sn'] = $order->sn;
                $datas['pay_price'] = $order->pay_price;
                $datas['order_type'] = $order->order_type;
                $datas['subject'] = $order_goods->goods_name .'咨询支付';
                $res = (new PaymentController())->doPay($datas);
                if($res){
                    return $this->ResultJson(1, "支付宝支付成功!", ['str'=>$res]);
                }
                return $this->ResultJson(0, "支付失败!", []);

            } elseif ($param['pay_type'] == 3) { //余额
                $makeMoney = new MakeMoney();
                $res = $makeMoney->doSurplusPay($param);
                if ($res === true) {
                    return $this->ResultJson(1, "余额支付成功!", []);
                }
                return $this->ResultJson(0, $res, []);
            }
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取售后详情
     */
    public function getAfterSalesDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['after_id'])) {
            return $this->ResultJson(0, "售后详情ID不能为空!", []);
        }
        $res = (new AfterSales())->getAfterSalesInfo($param['after_id']);
        return $this->ResultJson(1, "获取成功!", $res->toArray());
    }
    
    
    /***
     * 拒绝售后
     * 
     */
    public function rejectReason(Request $request)
    {
        $param = $request->input();
        if (empty($param['after_id'])) {
            return $this->ResultJson(0, "售后详情ID不能为空!", []);
        }
        if (empty($param['reject_reason'])) {
            return $this->ResultJson(0, "拒绝原因不能为空!", []);
        }
        $res = (new AfterSales())->doRejectReason($param);
        if ($res) {
            return $this->ResultJson(1, "驳回成功!", []);
        } else {
            return $this->ResultJson(0, "驳回失败!", []);
        }
    }


    /***
     * 同意售后
     *
     */
    public function agree(Request $request)
    {
        $param = $request->input();
        if (empty($param['after_id'])) {
            return $this->ResultJson(0, "售后详情ID不能为空!", []);
        }
        $res = (new AfterSales())->doAgree($param);
        if ($res === true) {
            return $this->ResultJson(1, "确认成功!", []);
        } else {
            return $this->ResultJson(1, "退款处理中!", []);
        }
    }

    /***
     * 发货
     */
    public function delivery(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        if (empty($param['logis_company'])) {
            return $this->ResultJson(0, "物流公司名称不能为空!", []);
        }
        if (empty($param['logis_num'])) {
            return $this->ResultJson(0, "物流单号不能为空!", []);
        }
        if (empty($param['logis_code'])) {
            return $this->ResultJson(0, "物流编码不能为空!", []);
        }
        $res = (new Order())->delivery($param);
        if ($res ===true) {
            return $this->ResultJson(1, "发货成功!", []);
        } else {
            return $this->ResultJson(0, $res, []);
        }
    }


    /***
     * 取消订单
     */
    public function cancel(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
//        if (empty($param['cancel_type'])) {
//            return $this->ResultJson(0, "取消类型不能为空!", []);
//        }
        if (!isset($filter['types']) && empty($filter['types'])) {
            if (empty($param['cancel_reason'])) {
                return $this->ResultJson(0, "取消原因不能为空!", []);
            }
        }
        $res = (new Order())->cancel($param);
        if ($res ===true) {
            return $this->ResultJson(1, "取消成功!", []);
        } else {
            return $this->ResultJson(0, $res, []);
        }
    }

    /**
     * @param Request $request
     * 医护端咨询管理列表
     */
    public function getDoctorConsultList(Request $request)
    {
        $param = $request->input();
        if (empty($param['work_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空!", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空!", []);
        }
        $res = (new Order())->getDoctorConsultList($param);
        if ($res) {
            return $this->ResultJson(1, "获取成功!", $res);
        } else {
            return $this->ResultJson(0, $res, []);
        }
    }



    /**
     * @param Request $request
     * 医护端咨询管理===医师加时
     */
    public function addTime(Request $request)
    {
        $param = $request->input();
        if (empty($param['group_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空!", []);
        }
        if (empty($param['minute'])) {
            return $this->ResultJson(0, "分钟数不能为空!", []);
        }
        $res = (new Order())->addTime($param);
        if ($res === true) {
            return $this->ResultJson(1, "加时成功!", []);
        } else {
            return $this->ResultJson(0, $res, []);
        }
    }


    /**
     * @param Request $request
     * 医护端咨询管理===确认接单
     */
    public function confirm(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空!", []);
        }
        $res = (new Order())->confirm($param);
        if ($res === true || is_numeric($res)) {
            return $this->ResultJson(1, "接单成功!", ["group_id"=>is_numeric($res)?$res:0]);
        } else {
            return $this->ResultJson(0, $res, []);
        }
    }

  /**
     * @param Request $request
     * 医护端咨询管理===开始咨询
     */
    public function startConsult(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空!", []);
        }
        $res = (new Order())->startConsult($param);
        if (is_array($res)) {
            return $this->ResultJson(1, "开始咨询成功!",$res);
        } else {
            return $this->ResultJson(0, $res, []);
        }
    }

    /**
     * @param Request $request
     * 医护端咨询管理===患者详情
     */
    public function patientDetail(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空!", []);
        }
        if (empty($param['doctor_id'])) {
            return $this->ResultJson(0, "医师ID不能为空!", []);
        }
        $res = (new Order())->patientDetail($param);
        if (is_array($res)) {
            return $this->ResultJson(1, "获取成功!", $res);
        } else {
            return $this->ResultJson(0, $res, []);
        }
    }
    
    
    /**
     *退款 
     * 
     */
    public function backMoney(Request $request)
    {
        $res = $request->input();
        if (empty($res['order_id'])) {
            return $this->ResultJson(0, "订单号不能为空", []);
        }
        $order = (new Order())->where([['id', '=', $res['order_id']]])->first();
        if (empty($order)) {
            return $this->ResultJson(0, "该订单不存在!", []);
        }
        $lit['sn'] = $order->sn;
        $lit['money'] = $order->order_price;
        $lit['back_sn'] = (new Order())->orderNumber();
        $result = (new PaymentController())->doReturn($lit);
        if ($result === true) {
            (new Order())->where([['id', '=', $res['order_id']]])->update(['order_status'=>4]);
            return $this->ResultJson(1, "退款成功!", []);
        }
        return $this->ResultJson(0, "退款失败!", []);
    }


    /***
     * 根据群ID获取剩余时间
     */
    public function getTimeByGroupId(Request $request)
    {
        $res = $request->input();
        if (empty($res['group_id'])) {
            return $this->ResultJson(0, "工作室ID不能为空", []);
        }
         $da = Order::getTimeBYGroupid($res);
        if(is_array($da)){
            return $this->ResultJson(1, "获取成功!", ['time'=>$da['time'],'doc_name'=>$da['doc_name']]);
        }
        return $this->ResultJson(0, $da, []);
        
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 获取退货地址
     */
    public function getReturnAddress(Request $request)
    {
        $param = $request->input();
        if (empty($param['order_id'])) {
            return $this->ResultJson(0, "订单ID不能为空", []);
        }
        $res = (new Order())->getReturnAddress($param);
        if(is_array($res)){
            return $this->ResultJson(1, "获取成功!", $res);
        }
        return $this->ResultJson(0, $res, []);
    }

}
